package com.zhendong.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;


/**
 * 1、导入依赖
 * 2、编写配置，给容器中注入一个RestHighLevelClient
 */
@SpringBootConfiguration
public class GulimallElasticSearchConfig {

    /**
     * 因为是默认的设置项，如果没有特殊设置的话这个可以不写
     */
    public static final RequestOptions COMMON_OPTIONS;   //基于默认规则加的设置项
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);   //每一次请求都可以让它带上令牌
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    RestHighLevelClient client() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.56.10", 9200,
                "http"));
        return new RestHighLevelClient(builder);
    }

}
